<template>
  <div>
  <el-row class="add-button">
  <el-button type="primary" @click="dialogFormVisible = true" icon="el-icon-circle-plus">新增</el-button>
  <!-- <el-button type="danger" icon="el-icon-delete-solid">移除</el-button> -->
  </el-row>
  <el-dialog title="新增角色信息" :visible.sync="dialogFormVisible"  :close-on-click-modal="false"
    @close="clear">
  <el-form :model="roleForm" :rules="rules" v-loading="loading">
    <el-form-item label="角色名" :label-width="formLabelWidth" prop="name">
      <el-input v-model="roleForm.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="角色描述" :label-width="formLabelWidth" prop="password">
      <el-input v-model="roleForm.nameZh" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item prop="username">
          <el-tag>初始权限：无</el-tag>
     </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" v-on:click="createRole">确 定</el-button>
  </div>
</el-dialog>
</div>
</template>

<script>
  export default {
    name: 'RoleCreate',
    data () {
      return {
        dialogFormVisible: false,
        rules: {
          name: [{required: true, message: '角色名不能为空', trigger: 'blur'}]
        },
        checked: true,
        roleForm: {
          name: '',
          nameZh: ''
        },
        loading: false
      }
    },
    methods: {
      clear () {
        this.roleForm = {
          name: '',
          nameZh: ''
        }
      },
      createRole () {
        this.$loading({
                  lock: true,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                })
        this.$axios
          .post('/admin/role', {
            name: this.roleForm.name,
            nameZh: this.roleForm.nameZh
          })
          .then(resp => {
            if (resp.data.code === 200) {
              this.$loading({
                  lock: false,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                }).close();
              this.$message.success(resp.data.result)
              this.clear()
              this.$emit('onSubmit')
            } else {
              this.$loading({
                  lock: false,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                }).close();
              this.$message.warning(resp.data.message)
            }
          })
          .catch(failResponse => {
            this.$loading({
                  lock: false,
                  text: "Loading",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
                }).close();
            console.error(JSON.stringify(failResponse))
          })
        this.dialogFormVisible = false
      }
    }
  }
</script>

<style scoped>
  .add-button {
    margin-right: 90%;
    margin-top: 2%;
  }
</style>
